package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsGrouponRulesMapper;
import com.qf.pojo.DtsGoods;
import com.qf.pojo.DtsGrouponRules;
import com.qf.service.DtsGrouponRulesService;
import com.qf.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * projectName:workspace_6
 * author:hyn
 * time:2021/12/1 16:21
 * description:
 */
@Service
public class DtsGrouponRulesServiceImpl implements DtsGrouponRulesService {
    @Autowired
    private DtsGrouponRulesMapper grouponRulesMapper;
    @Override
    public IPage<DtsGrouponRules> findAll(String goodsId, Integer limit, Integer page, String sort, String order) {
        IPage<DtsGrouponRules> ipage = new Page(page,limit);
        QueryWrapper<DtsGrouponRules> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(goodsId)){
            wrapper.like("goods_id",goodsId);
        }
        if("asc".equals(order)){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }
        IPage<DtsGrouponRules> p = grouponRulesMapper.selectPage(ipage, wrapper);
        return p;
    }

    @Override
    public DtsGrouponRules add(String discount, String discountMember, Date expiretime, DtsGoods goods) {

        DtsGrouponRules grouponRules = new DtsGrouponRules();
        grouponRules.setExpireTime(expiretime);
        grouponRules.setAddTime(new Date());
        grouponRules.setUpdateTime(new Date());
        grouponRules.setDiscount(new BigDecimal(discount));
        grouponRules.setDiscountMember(Integer.parseInt(discountMember));
        grouponRules.setPicUrl(goods.getPicUrl());
        grouponRules.setGoodsId((long)goods.getId());
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setDeleted(false);

        grouponRulesMapper.insert(grouponRules);
        return grouponRules;
    }

    @Override
    public Object save(DtsGrouponRules grouponRules, DtsGoods goods) {
//        QueryWrapper<DtsGrouponRules> wrapper = new QueryWrapper<>();
        DtsGrouponRules dtsGrouponRules = grouponRulesMapper.selectById(grouponRules);
        if(dtsGrouponRules.getExpireTime().getTime() != grouponRules.getExpireTime().getTime()){
            return ResponseUtil.fail(-1,"团购未结束，不能修改团购结束时间");
        }

        if(dtsGrouponRules.getGoodsId().intValue() != goods.getId()){
            return ResponseUtil.fail(-1,"团购未结束，不能修改团购商品,原商品Id："+dtsGrouponRules.getGoodsId());
        }
        if(dtsGrouponRules.getDiscount().compareTo(grouponRules.getDiscount()) != 0){
            return ResponseUtil.fail(-1,"团购未结束，不能修改团购折扣,原折扣"+dtsGrouponRules.getDiscount());
        }

        if(dtsGrouponRules.getDiscountMember().compareTo(grouponRules.getDiscountMember()) != 0){
            return ResponseUtil.fail(-1,"团购未结束，不能修改团购人数,原人数"+dtsGrouponRules.getDiscountMember());
        }



        grouponRules.setUpdateTime(new Date());
        grouponRules.setGoodsName(goods.getName());
        grouponRules.setPicUrl(goods.getPicUrl());
        grouponRules.setGoodsId((long)goods.getId());
        grouponRulesMapper.updateById(grouponRules);

        return ResponseUtil.ok();
    }

    @Override
    public Object delete(DtsGrouponRules grouponRules) {

        Date date = new Date();
        Date expireTime = grouponRules.getExpireTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        if(date.getTime() < expireTime.getTime()){
            return ResponseUtil.fail(-1,"团购未结束");
        }

//        grouponRulesMapper.deleteById(grouponRules);
        return ResponseUtil.ok();
    }
}
